package org.vectrics.recruiter.reporting;



import java.awt.Color;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.BaseFont;
import org.apache.log4j.Logger;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.PhraseManager;
import org.vectrics.VectricsException;
import org.vectrics.common.util.DateUtil;
import org.vectrics.domain.Domain;
import org.vectrics.history.HistoryProvider;
import org.vectrics.history.Activity;
import java.util.Iterator;
import java.util.Locale;
import java.text.*;
import java.util.Date;


/**
 * 
 * @author mmoore
 */
public class TransactionReport extends RecruitBaseReport {
	private transient static Logger log = Logger.getLogger(TransactionReport.class);
    
	
	private SimpleDateFormat historyDateFormat = new SimpleDateFormat("MM-dd-yyyy");
	
	private PdfPTable historyTable = new PdfPTable(5);
	private BaseFont FOOTER_FONT = null;
	private Color ACTIVE_COLOR = new Color(255, 248, 210);
	private Date endDate = new Date(); 
	private Date startDate = new Date(endDate.getTime() - (DateUtil.MILLIS_IN_A_DAY * 5)); 
	
	
	public TransactionReport(Domain domain, Locale locale) {
		super(domain, locale);
		try {
			FOOTER_FONT = 
				BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
		}
		catch (Exception de) {
			log.error("Exception creating footer font", de);
		}
	}

	protected Rectangle getPageSize() {
		return(PageSize.A4.rotate());
	}


	 
	protected void fill(Document document) throws VectricsException {
		log.debug("fill");
		try {
			
			document.add(new Paragraph("Transaction Report    >> " + startDate + " to " + endDate, ReportConstants.titleFont));

			log.debug("fill() - setting table properties");
			historyTable.setHorizontalAlignment(Element.ALIGN_LEFT);
			historyTable.setWidthPercentage(100);
			int historyTableWidths[] = {80, 60, 60, 60, 150};
			historyTable.setWidths(historyTableWidths);

			log.debug("fill() - adding table header row");
			addTableHeaderCell(historyTable, "prompt.time.stamp");
			addTableHeaderCell(historyTable, "prompt.user.name");
			addTableHeaderCell(historyTable, "prompt.data.type");
			addTableHeaderCell(historyTable, "prompt.action.type");
			addTableHeaderCell(historyTable, "prompt.description");
			historyTable.setHeaderRows(1);
			
			log.debug("fill() - adding user rows");
			Iterator iter = HistoryProvider.getInstance().findHistoryEntries(startDate, endDate).iterator();


			while (iter.hasNext()) {
				
				Activity hist = (Activity)iter.next();

				addHistoryRow(hist);
			}
			log.debug("fill() - adding user table to document");
			document.add(historyTable);

		}
		catch(Exception e) {
			log.error("Exception creating pdf document - in fill()", e);
			throw new VectricsException("Exception creating pdf document - fill()", e);
		}
        
	}



    
	protected void setupHeaderFooter(Document document) throws VectricsException {
	}


	protected void addTableHeaderCellCode(String titleCode) {
		String title = CoreServiceLocator.getPhraseProvider().getMessage(titleCode, this.getLocale());
		log.debug("addTableHeaderCell()");
		PdfPCell hdrCell = new PdfPCell(new Phrase(title, ReportConstants.stdBoldFont));
		hdrCell.setBackgroundColor(ReportConstants.TABLE_HEAD_COLOR);
		this.historyTable.addCell(hdrCell);	
	}
	   

	protected void addHistoryRow(Activity history) {
		Color color = new Color(255, 255, 255);
		
		PdfPCell cell = new PdfPCell(new Phrase(history.getDate().toString(), ReportConstants.stdFont));
		cell.setBackgroundColor(color);
		this.historyTable.addCell(cell);	

		cell = new PdfPCell(new Phrase(history.getPerson().getName(), ReportConstants.stdFont));
		cell.setBackgroundColor(color);
		this.historyTable.addCell(cell);	

		cell = new PdfPCell(new Phrase(history.getObjectType(), ReportConstants.stdFont));
		cell.setBackgroundColor(color);
		this.historyTable.addCell(cell);	

		cell = new PdfPCell(new Phrase(history.getActivityType().getDescription(), ReportConstants.stdFont));
		cell.setBackgroundColor(color);
		this.historyTable.addCell(cell);	

		String notes = "";
		cell = new PdfPCell(new Phrase(history.getDescription(), ReportConstants.stdFont));
		cell.setBackgroundColor(color);
		this.historyTable.addCell(cell);	
	}   
	
	

	/**
	 * @return
	 */
	public Date getEndDate() {
		return endDate;
	}

	/**
	 * @param endDate
	 */
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	/**
	 * @return
	 */
	public Date getStartDate() {
		return startDate;
	}

	/**
	 * @param startDate
	 */
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

}
